<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性模板</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="135px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="ruleForm.pinyin"></el-input>
      </el-form-item>
      <el-form-item label="关键词列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        pinyin:'',
        keywords:'',
        sort:'',
      },
      rules: {
        name: [
          {required: true, message: '请输入名字', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        pinyin: [
          {required: true, message: '请输入拼音', trigger: 'blur'},
          {min: 4, max: 50, message: '长度在 4 到 50 个字符', trigger: 'blur'}
        ],
        keywords: [
          {required: false, message: '请输入关键词', trigger: 'blur'},
          {min: 4, max: 50, message: '长度在 4 到 50 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: false, message: '请输入排序序号', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/brands/add-new'
          console.log('url='+url)
          let formData =this.qs.stringify(this.ruleForm)
          console.log('formData='+formData)
          this.axios
              .create({
                'headers':{
                  'Authorization': localStorage.getItem('jwt')
                }
              })
              .post(url,formData).then((response) => {
            let responseBody = response.data;
            console.log('responseBody:')
            console.log(responseBody)
            if(responseBody.state==1){
              this.$message({
                    message: "添加成功",
                    type: "success"
                  },
                  this.$refs[formName].resetFields()
              )
            }else {
              this.$message.error(responseBody.message)
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>